package com.chuantou.yuba.ui;

import java.util.HashMap;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.chuantou.yuba.R;
import com.chuantou.yuba.api.RequestManager;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.BaseTO;
import com.chuantou.yuba.util.Constants;
import com.chuantou.yuba.util.UiUtil;

public class ResetPasswordActivity extends BaseActionBarActivity implements OnClickListener {
	private static final int WHAT_REMOVE_ERROR_MSG = 1;
	private TextView mErrorView;
	private Button mSubmitButton;
	private EditText mPasswordView;
	private EditText mRepasswordView;
	private ProgressDialog mFindDialog;
	private String mPhone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reset_password);
		mPhone = getIntent().getStringExtra(Constants.EXTRA_PHONE);
		setupViews();
		setTitle(getString(R.string.reset_passwprd));
		if (TextUtils.isEmpty(mPhone)) {
			finish();
		}
	}

	private void setupViews() {
		mPasswordView = (EditText) findViewById(R.id.password);
		mRepasswordView = (EditText) findViewById(R.id.repassword);
		mErrorView = (TextView) findViewById(R.id.message);
		mSubmitButton = (Button) findViewById(R.id.submit);
		mSubmitButton.setOnClickListener(this);
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case WHAT_REMOVE_ERROR_MSG:
				if (!hasDestroyed()) {
					mErrorView.setVisibility(View.GONE);
				}
				break;
			default:
				break;
			}
		};
	};

	private void showErrorMessage(String msg) {
		mHandler.removeMessages(WHAT_REMOVE_ERROR_MSG);
		mErrorView.setVisibility(View.VISIBLE);
		mErrorView.setText(msg);
		mHandler.sendEmptyMessageDelayed(WHAT_REMOVE_ERROR_MSG, 1500);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit:
			changePassword();
			break;
		default:
			break;
		}
	}

	private void changePassword() {
		if (UiUtil.isFastDoubleClick())
			return;
		String newPassword = mPasswordView.getText().toString();
		if (newPassword.length() < 6) {
			showErrorMessage(getString(R.string.input_new_password_error));
			return;
		}
		String rePassword = mRepasswordView.getText().toString();
		if (!newPassword.equals(rePassword)) {
			showErrorMessage(getString(R.string.input_repassword_error));
			return;
		}
		showFindDialog();
		String url = UriHelper.getResetPasswordByPhoneUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_MOBILE, mPhone);
		params.put(UriHelper.PARAMS_NEW_PASS, newPassword);
		GsonRequest<BaseTO> request = new GsonRequest<BaseTO>(Method.POST, url, new Listener<BaseTO>() {

			@Override
			public void onResponse(BaseTO response) {
				if (!hasDestroyed()) {
					dismissFindDialog();
					if (response == null) {
						showErrorMessage(getString(R.string.server_error));
					} else {
						if (response.isSuccess()) {
							Toast.makeText(getApplicationContext(), R.string.find_password_success, Toast.LENGTH_SHORT).show();
							finish();
						} else {
							String msg = response.getMsg();
							msg = TextUtils.isEmpty(msg) ? getString(R.string.find_password_error) : msg;
							showErrorMessage(msg);
						}
					}
				}
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				if (!hasDestroyed()) {
					dismissFindDialog();
					showErrorMessage(getString(R.string.request_error));
				}
			}
		}, params, BaseTO.class);
		RequestManager.startRequest(getApplicationContext(), request);
	}

	private void showFindDialog() {
		if (mFindDialog != null && mFindDialog.isShowing()) {
			return;
		}
		mFindDialog = new ProgressDialog(this);
		mFindDialog.setMessage(getString(R.string.submiting));
		mFindDialog.show();
	}

	private void dismissFindDialog() {
		if (mFindDialog != null && mFindDialog.isShowing()) {
			mFindDialog.dismiss();
		}
	}
}
